<?php 
/*
 * 登录模块
 */


include_once 'common_mbs.inc.php';
include_once './smarty.inc.php';
include_once 'libs/securimage/securimage.php';

function loginalert($content, $second = 0)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	echo "<script>alert('" . $content . "');setTimeout('history.go(-1)', " . $second . ");</script>";
}

$op = trim($_REQUEST['op']);
$t = md5(uniqid());

if ( $op == "login" )
{
	$admin_name = isset($_POST['userid']) ? trim($_POST['userid']) : '';
	$password   = isset($_POST['pwd']) ? md5(trim($_POST['pwd'])) : '';
	$validate   = isset($_POST['validate']) ? trim($_POST['validate']) : '';
	
	if (empty($admin_name) || empty($password) || empty($validate))
	{
		loginalert("请填写完整资料！");
		exit();
	}
	
	$image = new Securimage();
	if (!$image->check($validate))
	{
		loginalert("验证码错误！");
		exit();
	}	
	$login_str  = "admin_name='{$admin_name}'";
	$admin_index_obj  = new osgh_admin_index_tbl_cls();
	$login_result = $admin_index_obj->getAdminIndexList(FALSE, $login_str);	
	if (count($login_result) != 1)
	{
		loginalert("账号错误！");
		exit();
	}
	
	$admin_info = $login_result[0];
	if ($admin_info['password'] != $password)
	{
		loginalert("密码错误！");
		exit();
	}
	if ($admin_info['is_available'] == 0)
	{
		loginalert('该账号已被禁用!');
		exit();
	}
	
	
	$admin_id   = intval($admin_info['admin_id']);
	$admin_name = trim($admin_info['admin_name']);
	$real_name  = trim($admin_info['real_name']);	
	$group_id   = intval($admin_info['group_id']);
	$cinemaid   = trim($admin_info['cinemaid']);
	
	$_SESSION['MBS_USER'] = array(
		'admin_id' =>  $admin_id,
		'admin_name' => $admin_name,
		'real_name' => $real_name,		
		'group_id' => $group_id,
		'cinemaid' => $cinemaid
	);
	
	$admin_index_obj->updateAdminIndex(array('login_time'=>date('Y-m-d H:i:s')),"admin_id='{$admin_id}'"); 
	header("Location: index.php");
	exit();
}
elseif ( $op == "logout" )
{
	unset($_SESSION['MBS_USER']);
	tcmc_cls::ShowMsg("你已成功登出", "login.php");
}
else
{	
	$smarty->assign('t',time());
	$smarty->display('login.tpl');
}

?>